package com.weilf.zookeeper.config;

import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;

/**
 * zookeeper 工具类
 * @author weil-f
 * @date 2021/2/7 8:18
 */
public class ZkUtils {
	private static ZooKeeper zk;

	/**
	 * zk 的连接地址，应该使用集群，这里简单演示，用的单击
	 */
	private static String connect = "192.168.6.29:2181/testConfig";

	private static DefaultWatcher deafultWatch = new DefaultWatcher();

	private static CountDownLatch countDownLatch  = new CountDownLatch(1);

	public static ZooKeeper instance() throws Exception {
		zk =  new ZooKeeper(connect,3000,deafultWatch);
		deafultWatch.setCountDownLatch(countDownLatch);
		// 这里阻塞
		countDownLatch.await();
		return zk;
	}

}
